package org.elepower.controller;

import org.elepower.dao.FunctionDao;
import org.elepower.dao.RoleDao;
import org.elepower.pojo.PsFunction;
import org.elepower.pojo.PsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/function")
public class FunctionController {

    @Autowired
    private FunctionDao functionDao;
    @Autowired
    private RoleDao roleDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "system/function/index";
    }

    /**
     * 查询是否有角色在使用该权限
     * @param funId
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectRoleByFunId")
    @ResponseBody
    public int selectRoleByFunId(Integer funId) throws Exception{
        return roleDao.getByFunId(funId);
    }
    /**
     * 查询所有权限
     * @return
     * @throws Exception
     */
    @RequestMapping("/select")
    @ResponseBody
    public Map<String,Object> select() throws Exception{
        Map<String,Object> map = new HashMap<>();
        List<PsFunction> list = functionDao.getAll();
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);
        return map;
    }

    /**
     * 返回添加权限页面
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addParent",method = RequestMethod.GET)
    public String addParent() throws Exception{
        return "system/function/functionEdit";
    }

    /**
     * 下拉菜单查询所有权限
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectParent")
    @ResponseBody
    public List<PsFunction> selectParent() throws Exception{
        return functionDao.getAll();
    }

    /**
     * 添加权限方法
     * @param psFunction
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addParent",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addParent(PsFunction psFunction, HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();
        PsUser psUser = (PsUser)session.getAttribute(Constants.LOGIN_USER);
        psFunction.setCreatedBy(psUser.getUsername());
        psFunction.setLastUpdateTime(new Date());
        if(psFunction.getIsStart() == null){
            psFunction.setIsStart(0);
        }
        functionDao.add(psFunction);
        map.put("result",true);
        return map;
    }

    /**
     * 根据父权限编号获取权限信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/getFunction")
    @ResponseBody
    public List<PsFunction> getFunction(Integer id) throws Exception{
        return functionDao.getFunByParentId(id);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<>();
        roleDao.deleteByFunId(id);
        functionDao.del(id);
        map.put("result",true);
        return map;
    }

    /**
     * 获取制定权限信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/get")
    @ResponseBody
    public PsFunction get(Integer id) throws Exception{
        return functionDao.get(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsFunction psFunction) throws Exception{
        Map<String,Object> map = new HashMap<>();

        psFunction.setLastUpdateTime(new Date());

        if(psFunction.getIsStart() == null){
            psFunction.setIsStart(0);
        }

        functionDao.update(psFunction);
        map.put("result",true);
        return map;
    }

    /**
     * 渲染ZTree的方法
     * @return
     * @throws Exception
     */
    @RequestMapping("/functionList")
    @ResponseBody
    public List<PsFunction> functionList() throws Exception{
        return functionDao.getFunctionList();
    }
}
